//
//  WKLoginHandel.m
//  WeKeTang
//
//  Created by ichina on 2017/9/4.
//  Copyright © 2017年 Dezhi Tiancheng Education Technology (Beijing) Co. Ltd. All rights reserved.
//

#import "WKLoginHandel.h"

@implementation WKLoginHandel

+ (instancetype)shareLoginHandler{
    
    static WKLoginHandel *_loginHandler;
    
    static dispatch_once_t onceHandler;
    
    dispatch_once(&onceHandler, ^{
        
        _loginHandler = [[self alloc]init];
        
    });
    return _loginHandler;
}
+(void)saveLoginDataWithDict:(NSDictionary*)dict{
    
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *path = [doc stringByAppendingPathComponent:@"account.plist"];
    
    NSDictionary *dic = @{@"userid": [NSString stringWithFormat:@"%@",dict[@"userid"]],
                          @"avatar":[NSString stringWithFormat:@"%@",dict[@"avatar"] ],
                          @"account":[NSString stringWithFormat:@"%@",dict[@"account"]],
                          @"nickname":[NSString stringWithFormat:@"%@",dict[@"nickname"]],
                          @"SESSID":[NSString stringWithFormat:@"%@",dict[@"SESSID"] ],
                          @"hostdate":[NSString stringWithFormat:@"%@",dict[@"hostdate"]],
                         @"phoneNum":[NSString stringWithFormat:@"%@",dict[@"phoneNum"]],
                        @"password":[NSString stringWithFormat:@"%@",dict[@"password"]]
                          };
     NSLog(@"沙盒路径2:%@",path);
    BOOL succesee = [dic writeToFile:path atomically:YES];
    
    if (succesee == 1) {
        
        NSLog(@"保存成功");
    }else{
        NSLog(@"保存失败");
    }

}
+(NSString*)getLoginDataWithString:(NSString*)string{
    
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    
    NSString *path = [doc stringByAppendingPathComponent:@"account.plist"];
    
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
    
    return dict[string];

}
+ (void)removeLoginData{
    
    NSFileManager* fileManager=[NSFileManager defaultManager];
    
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *path = [doc stringByAppendingPathComponent:@"account.plist"];
    BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:path];
    if (!blHave) {
        NSLog(@"没找到文件");
        return ;
    }else {
        NSLog(@" 找到文件");
        BOOL blDele= [fileManager removeItemAtPath:path error:nil];
        if (blDele) {
            NSLog(@"删除沙盒文件成功");
        }else {
            NSLog(@"删除沙盒文件失败");
        }
    }
}

@end
